<?php
class SearchModule
{
	function index() {
		global $_G;
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        if (!$id)
        {
        	$cates = $_G['cache']->get('category');
	        $limit = 15;
			$articles = array();
			if(ican('view_hidden_category')) {
				$status = '1';
			}else{
				$status = 'c.status=0';
			}
			$sql = "SELECT a.*,c.catename FROM ".DB_PREFIX."article a LEFT JOIN ".DB_PREFIX."category c ON a.cid=c.cid WHERE c.type=0 AND $status AND a.ischeck=1 AND a.dateline < ".$_G['timestamp']." ORDER BY a.aid DESC ";
			$query = $_G['db']->page_query($sql,$limit);
			$num = $_G['db']->result("SELECT count(*) as num FROM ".DB_PREFIX."article a LEFT JOIN ".DB_PREFIX."category c ON a.cid=c.cid WHERE c.type=0 AND $status AND a.ischeck=1 AND a.dateline < ".$_G['timestamp']." ORDER BY a.aid DESC ");
			while($data = $_G['db']->fetch_array($query)) {
				$articles[] = $data ;
			}
			$pager = pager($num,$limit);
			include template('article');
        } else {
        	$cookie = getGP('article_views'.$id,'c');
        	if(TIMESTAMP - $cookie > 600) {
        		set_cookie('article_views'.$id,TIMESTAMP,1000);
        		$_G['db']->query("UPDATE ".DB_PREFIX."article SET views=views+1 WHERE aid=".$id);
        	}
	        $article = $_G['cache']->get($id,'article');
	        $comments = $_G['cache']->get($id,'comment');
	        if( !$article) {
	        	$article = $_G['db']->fetch('article','*',array('aid'=>$id));
	        	if($article['aid'] && $article['dateline'] < TIMESTAMP) {
	        		$_G['cache']->set($id,$article,'article');
	        	}else{
	        		$article = array();
	        	}
	        }
	        check_access('viewcagegory');
	        if(!$comments) {
			    $query = $_G['db']->query("SELECT c.* FROM ".table('comment')."c WHERE c.referid='$id' AND c.type=0");
				while($row = $_G['db']->fetch_array($query)) {
					$comments[$row['cid']] = $row;
				}
	        	foreach($comments as $k => $v){
	        		if($v['pid'] > 0){
	        			$comments[$v['pid']]['subcomment'][$v['cid']] = $v;
	        			$comments[$k] = null;
	        			unset($comments[$k]);
	        		}
	        	}
	        	$_G['cache']->set($id,$comments,'comment');
	        }
	        $_G['title'] = $article['title'];
	        $class['cate'.$article['cid']]=1;
	        if($article['summary']){
	        	$_G['description'] = strip_tags($article['summary']);
	        }else{
	        	$_G['description'] = $article['title'];
	        }
	        
	        include template('article_view');
        }
	}

	function tags(){
		global $_G;
		$tag=getGP('a','G');
		if($tag){
			$limit = 15;
			$tagid = $_G['db']->result("SELECT tagid FROM ".table('tags')." WHERE tagname = '$tag'");
			!$tagid && $tagid = '0';
			$wheresql .= " AND (a.tags = '$tagid' OR a.tags LIKE '{$tagid},%' OR a.tags LIKE '%,{$tagid}' OR a.tags LIKE '%,{$tagid},%')";
		
			$sql = "SELECT * FROM ".table('article')." a WHERE a.ischeck = 1 AND a.dateline < ".$_G['timestamp']." $wheresql ORDER BY a.dateline DESC ";
			$query = $_G['db']->page_query($sql,$limit);
			$num = $_G['db']->result("SELECT count(*) AS num FROM ".table('article')." a WHERE a.ischeck = 1 AND a.dateline < ".$_G['timestamp']." $wheresql ORDER BY a.dateline DESC ");
			
			while($data = $_G['db']->fetch_array($query)) {
				$articles[] = $data ;
			}
			$_G['title']=$tag;
			$pager = pager($num,$limit);
			include template('article');
		}else{
			include template('article');
		}
	}
	

}	